package udes.assassin;

import java.util.List;

import udes.assassin.structure.Player;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.TextView;

public class ScoreListAdapter extends ArrayAdapter<Player>{
	
	private final List<Player> list;
	private final Activity context;
	
	public ScoreListAdapter(Activity context, List<Player> list) {
	    super(context, R.layout.score_description, list);
	    this.context = context;
	    this.list = list;
	}

	static class ViewHolder {
	    protected TextView playerPosition;
	    protected TextView playerName;
	    protected TextView playerScore;
	    protected CheckBox isAlive;
	}
	  
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		  
	    View view = null;
	    
	    if (convertView == null) {
	    	LayoutInflater inflator = context.getLayoutInflater();
		    view = inflator.inflate(R.layout.score_description, null);
		    final ViewHolder viewHolder = new ViewHolder();
		    viewHolder.playerPosition = (TextView) view.findViewById(R.id.leaderboard_player_position);
		    viewHolder.playerName = (TextView) view.findViewById(R.id.leaderboard_player_name);
		    viewHolder.playerScore = (TextView) view.findViewById(R.id.leaderboard_player_score);
		    viewHolder.isAlive = (CheckBox) view.findViewById(R.id.leaderboard_is_alive);
		    view.setTag(viewHolder);
	    }
	    else {
	    	view = convertView;
	    }
	    ViewHolder holder = (ViewHolder) view.getTag();
	    StringBuilder builder = new StringBuilder();
	    builder.append(position+1);
	    builder.append(".");
	    holder.playerPosition.setText(builder.toString());
	    
	    holder.playerName.setText(list.get(position).getName());
	    
	    builder = new StringBuilder();
	    builder.append(list.get(position).getScore());
	    builder.append(" pts");
	    holder.playerScore.setText(builder.toString());
	    holder.isAlive.setChecked(list.get(position).isAlive());
	    return view;
	}
}
